package itbour.onetouchshow.widget;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import itbour.onetouchshow.R;
import itbour.onetouchshow.bean.HomeDetailslBean;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.utils.SpUtils;


public class PopWindowMessageCenter extends PopupWindow {

    private static final String TAG = "PopWindowPrintProduct";
    public LinearLayout pwCancel, pwPhoneKefu, joinQQGroup;
    private Context mContext;
    private View mView, line;

    public PopWindowMessageCenter(Activity context,
                                  View.OnClickListener itemsOnClick) {
        super(context);
        this.mContext = context;

        Log.i(TAG, "FinishProjectPopupWindow 方法已被调用");

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.pw_message_center, null);

        pwPhoneKefu = mView.findViewById(R.id.pw_phone_kefu);
        pwCancel = mView.findViewById(R.id.pw_cancel);
        joinQQGroup = mView.findViewById(R.id.pw_join_qq_group);
        line = mView.findViewById(R.id.view_dividing_line);


        pwPhoneKefu.setOnClickListener(itemsOnClick);
        pwCancel.setOnClickListener(itemsOnClick);
        joinQQGroup.setOnClickListener(itemsOnClick);


        //设置PopupWindow的View
        this.setContentView(mView);
        //设置PopupWindow弹出窗体的宽
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        //设置PopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        //设置PopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.WHITE);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
    }

    public void checkQQAndKey() {
        HomeDetailslBean detailslBean = new Gson().fromJson(SpUtils.getInstance().getString(AppConst.APPBASEINFO), HomeDetailslBean.class);
        String key = null;
        if (detailslBean != null && !TextUtils.isEmpty(detailslBean.getAppControl().getqGroupUrl())) {
            key = detailslBean.getAppControl().getqGroupUrl();
        }
        List<String> packetNames = new ArrayList<>();
        packetNames.add("com.tencent.mobileqq");
        packetNames.add("com.tencent.minihd.qq");
        packetNames.add("com.tencent.qqlite");
        packetNames.add("com.tencent.mobileqqi");
        packetNames.add("com.tencent.tim");
        packetNames.add("com.tencent.eim");

        if (isAvilible(mContext, packetNames) && key != null) {
            joinQQGroup.setVisibility(View.VISIBLE);
            line.setVisibility(View.VISIBLE);
        } else {
            joinQQGroup.setVisibility(View.GONE);
            line.setVisibility(View.GONE);
        }
    }

    private boolean isAvilible(Context context, List<String> packageNameList) {
        // 获取packagemanager
        boolean contain = false;
        final PackageManager packageManager = context.getPackageManager();
        // 获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        // 用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<String>();
        // 从pinfo中将包名字逐一取出，压入pName list中
        if (packageInfos != null) {
            for (int i = 0; i < packageInfos.size(); i++) {
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        // 判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        for (int i = 0; i < packageNames.size(); i++) {
            for (int j = 0; j < packageNameList.size(); j++) {
                if (packageNames.get(i).equals(packageNameList.get(j))) {
                    contain = true;
                    break;
                }
            }
        }
        return contain;
    }
}
